home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Freeware / Programare / highlight / highlight-W32GUI-2.2-10b-Setup.exe / {app} / src / texgenerator.cpp < prev    next >
C/C++ Source or Header  |  2005-03-31  |  7KB  |  287 lines

  1. /***************************************************************************
  2.                           TexGenerator.cpp  -  description
  3.                              -------------------
  4.     begin                : Mit Jul 24 2002
  5.     copyright            : (C) 2002 by AndrΘ Simon
  6.     email                : andre.simon1@gmx.de
  7.  ***************************************************************************/
  8.  
  9. /***************************************************************************
  10.  *                                                                         *
  11.  *   This program is free software; you can redistribute it and/or modify  *
  12.  *   it under the terms of the GNU General Public License as published by  *
  13.  *   the Free Software Foundation; either version 2 of the License, or     *
  14.  *   (at your option) any later version.                                   *
  15.  *                                                                         *
  16.  ***************************************************************************/
  17.  
  18. #include "texgenerator.h"
  19.  
  20. namespace highlight {
  21.  
  22. TexGenerator::TexGenerator(const string &colourTheme):
  23.   CodeGenerator( colourTheme)
  24. {
  25.   styleTagOpen.push_back( "{\\hlstd ");
  26.   styleTagOpen.push_back( "{\\hlstr ");
  27.   styleTagOpen.push_back( "{\\hlnum ");
  28.   styleTagOpen.push_back( "{\\hlslc ");
  29.   styleTagOpen.push_back( "{\\hlcom ");
  30.   styleTagOpen.push_back( "{\\hlesc ");
  31.   styleTagOpen.push_back( "{\\hldir ");
  32.   styleTagOpen.push_back( "{\\hldstr ");
  33.   styleTagOpen.push_back( "{\\hlline ");
  34.   styleTagOpen.push_back( "{\\hlsym ");
  35.   for (int i=0;i<NUMBER_BUILTIN_STYLES; i++)    {
  36.     styleTagClose.push_back( "}");
  37.   }
  38.  
  39.   /*This makes TeX to use every par it encounters (the \\leavevmode has
  40.   no effect when TeX is in horizontal mode and when TeX is in vertical
  41.   mode, it switches it to horizontal mode).*/
  42.   newLineTag="\\leavevmode\\par\n";
  43.  
  44.   spacer = "\\ ";
  45.   maskWs=true;
  46.   excludeWs=true;
  47.   maskWsBegin = "{\\hlstd";
  48.   maskWsEnd = "}";
  49.   styleCommentOpen="%";
  50. }
  51.  
  52. TexGenerator::TexGenerator()
  53. {}
  54. TexGenerator::~TexGenerator()
  55. {}
  56.  
  57. string TexGenerator::formatStyleAttributes(const string & elemName,const ElementStyle & elem)
  58. {
  59.   ostringstream s;
  60.   s << "\\def\\hl"
  61.     << elemName
  62.     << "{";
  63.   if (elem.isBold())
  64.     s << "\\bf";
  65.   if (elem.isItalic())
  66.     s << "\\it";
  67.   s  <<  "\\textColor{"
  68.      << (elem.getColour().getTexRedValue())<<" "
  69.      << (elem.getColour().getTexGreenValue())<<" "
  70.      << (elem.getColour().getTexBlueValue())<<" 0.0}}\n";
  71.   return  s.str();
  72. }
  73.  
  74. string TexGenerator::getHeader(const string & title)
  75. {
  76.   ostringstream os;
  77.  
  78.   if (langInfo.getSyntaxHighlight()) {
  79.     if (includeStyleDef) {
  80.        os << "\n"<<getStyleDefinition();
  81.        os << CodeGenerator::readUserStyleDef();
  82.     }  else  {
  83.        os << "\\input "
  84.           << getStyleOutputPath()
  85.           << "\n\n";
  86.     }
  87.   }
  88.  
  89.   return os.str();
  90. }
  91.  
  92. void TexGenerator::printBody()
  93. {
  94.     *out << "{\n\\tt\n";
  95.  
  96.   processRootState();
  97.   *out << "}\n";
  98. }
  99.  
  100. string TexGenerator::getFooter()
  101. {
  102.   ostringstream os;
  103.   os << "\\bye\n"
  104.      << "% TeX generated by Highlight "
  105.      << HIGHLIGHT_VERSION
  106.      << ", "
  107.      << HIGHLIGHT_URL
  108.      << endl;
  109.   return os.str();
  110. }
  111.  
  112. string TexGenerator:: maskCharacter(unsigned char c)
  113. {
  114.   switch (c)
  115.     {
  116.     case '{':
  117.     case '}':
  118.       {
  119.         string m;
  120.         m = "$\\";
  121.         m += c;
  122.         m += "$";
  123.         return m;
  124.       }
  125.       break;
  126.     case '^':
  127.       return "{\\bf\\^{}}";
  128.       break;
  129.     case '_':
  130.       return "\\_{}";
  131.       break;
  132.     case '&':
  133.     case '$':
  134.     case '#':
  135.     case '%':
  136.       {
  137.         string m;
  138.         m = "\\";
  139.         m += c;
  140.         return m;
  141.       }
  142.       break;
  143.     case '\\':
  144.       return "$\\backslash$";
  145.       break;
  146.     case ' ':
  147.       return spacer;
  148.       break;
  149.     case '+':
  150.     case '-':
  151.     case '<':
  152.     case '>':
  153.     case '=':
  154.       {
  155.         string m;
  156.         m = "$\\mathord{";
  157.         m += c;
  158.         m += "}$";
  159.         return m;
  160.       }
  161.       break;
  162.     case AUML_LC:
  163.       return "\\\"a";
  164.       break;
  165.     case OUML_LC:
  166.       return "\\\"o";
  167.       break;
  168.     case UUML_LC:
  169.       return "\\\"u";
  170.       break;
  171.     case AUML_UC:
  172.       return "\\\"A";
  173.       break;
  174.     case OUML_UC:
  175.       return "\\\"O";
  176.       break;
  177.     case UUML_UC:
  178.       return "\\\"U";
  179.       break;
  180.     case AACUTE_LC:
  181.       return "\\'a";
  182.       break;
  183.     case EACUTE_LC:
  184.       return "\\'e";
  185.       break;
  186.     case OACUTE_LC:
  187.       return "\\'o";
  188.       break;
  189.     case UACUTE_LC:
  190.       return "\\'u";
  191.       break;
  192.     case AGRAVE_LC:
  193.       return "\\`a";
  194.       break;
  195.     case EGRAVE_LC:
  196.       return "\\`e";
  197.       break;
  198.     case OGRAVE_LC:
  199.       return "\\`o";
  200.       break;
  201.     case UGRAVE_LC:
  202.       return "\\`u";
  203.       break;
  204.     case AACUTE_UC:
  205.       return "\\'A";
  206.       break;
  207.     case EACUTE_UC:
  208.       return "\\'E";
  209.       break;
  210.     case OACUTE_UC:
  211.       return "\\'O";
  212.       break;
  213.     case UACUTE_UC:
  214.       return "\\'U";
  215.       break;
  216.     case AGRAVE_UC:
  217.       return "\\`A";
  218.       break;
  219.     case EGRAVE_UC:
  220.       return "\\`E";
  221.       break;
  222.     case UGRAVE_UC:
  223.       return "\\`O";
  224.       break;
  225.     case OGRAVE_UC:
  226.       return "\\`U";
  227.       break;
  228.     case SZLIG:
  229.       return "\\ss ";
  230.       break;
  231.  /*    #ifndef _WIN32
  232.  // skip  first byte of multibyte chracters
  233.     case 195:
  234.       return string("");
  235.       break;
  236. #endif*/
  237.  
  238.     default :
  239.       string m;
  240.       return m += c;
  241.     }
  242. }
  243.  
  244. string TexGenerator::getMatchingOpenTag(unsigned int styleID){
  245.   return "{\\hl"+langInfo.getKeywordClasses()[styleID]+" ";
  246.  }
  247.  
  248. string TexGenerator::getMatchingCloseTag(unsigned int styleID){
  249.   return "}";
  250. }
  251.  
  252.  
  253. string TexGenerator::getStyleDefinition()
  254. {
  255.     if (styleDefinitionCache.empty()){
  256.         ostringstream os;
  257.         os << formatStyleAttributes("std", docStyle.getDefaultStyle());
  258.         os << formatStyleAttributes("num", docStyle.getNumberStyle());
  259.         os << formatStyleAttributes("esc", docStyle.getEscapeCharStyle());
  260.         os << formatStyleAttributes("str", docStyle.getStringStyle());
  261.         os << formatStyleAttributes("dstr", docStyle.getDirectiveStringStyle());
  262.         os << formatStyleAttributes("slc", docStyle.getSingleLineCommentStyle());
  263.         os << formatStyleAttributes("com", docStyle.getCommentStyle());
  264.         os << formatStyleAttributes("dir", docStyle.getDirectiveStyle());
  265.         os << formatStyleAttributes("line", docStyle.getLineStyle());
  266.         os << formatStyleAttributes("sym", docStyle.getSymbolStyle());
  267.  
  268.         KeywordStyles styles = docStyle.getKeywordStyles();
  269.         for (KSIterator it=styles.begin(); it!=styles.end(); it++){
  270.             os << formatStyleAttributes(it->first, *(it->second));
  271.         }
  272.  
  273.         os <<  "% The special option is not supported by all dvi drivers\n"
  274.            << "\\special{background rgb "
  275.            << docStyle.getBgColour().getLatexRedValue() << " "
  276.            << docStyle.getBgColour().getLatexGreenValue() << " "
  277.            << docStyle.getBgColour().getLatexBlueValue() << "}";
  278.         os << "\n\\nopagenumbers\n"
  279.            << "\\input colordvi\n";
  280.         styleDefinitionCache=os.str();
  281.     }
  282.     return styleDefinitionCache;
  283. }
  284.  
  285.  
  286. }
  287.